/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Sep 30, 2013  9:57:04 AM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.database.dao.queue.impl
 * File Name : SyncQueueDaoImpl.java
 * TODO
 */
package com.tis.database.dao.queue.impl;

import java.util.Collections;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import promis.utility.CommonService;
import com.tis.database.dao.impl.AbstractDaoImpl;
import com.tis.database.dao.queue.SyncQueueDao;
import com.tis.database.entitys.queue.SyncQueue;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Sep 30, 2013 : 9:57:04 AM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
@Repository
@SuppressWarnings("unchecked")
public class SyncQueueDaoImpl extends AbstractDaoImpl<SyncQueue, String> implements SyncQueueDao  {

	
	private static final Logger logging = LoggerFactory.getLogger(SyncQueueDaoImpl.class);
	/**
	 * Date =Sep 30, 2013 : 10:18:34 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param entityClass
	 */
	protected SyncQueueDaoImpl() {
		super(SyncQueue.class);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.queue.SyncQueueDao#search(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 * Date =Sep 30, 2013 : 10:18:32 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */	
	@Override
	public List<SyncQueue> search(	String queueId, 
									String processModule, 
									String processingName, 
									String processLevel,
									String procCreatedonStart,
									String procCreatedonOnEnd) throws HibernateException, Exception {


		try {
			
			Session session = getCurrentSession();
			Query q = session.getNamedQuery(SyncQueue.findBySearch);
			String query = q.getQueryString(); 
			
			if(!CommonService.IsNull(queueId))
				query += " And upper(c.queueId) like '%"+ CommonService.IsNullRString(queueId).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(processModule))
				query += " And upper(c.processModule) like '%"+ CommonService.IsNullRString(processModule).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(processingName))
				query += " And upper(c.processingName) like '%"+ CommonService.IsNullRString(processingName).toUpperCase() +"%'"; 
			
			if(!CommonService.IsNull(processLevel))
				query += " And upper(c.processLevel) like '%"+ CommonService.IsNullRString(processLevel).toUpperCase() +"%'";
						
			if(!CommonService.IsNull(procCreatedonStart) && !CommonService.IsNull(procCreatedonOnEnd))
				query += " And to_char(c.procCreatedon, 'YYYY-MM-DD') BETWEEN :procCreatedonStart And :procCreatedonOnEnd ";
			
			query += " Order by :p_orderby "; 			

		
			logging.info(query);
			
			q = session.createQuery(query);
			if(!CommonService.IsNull(procCreatedonStart))
				q.setParameter("procCreatedonStart", CommonService.IsNullRString(procCreatedonStart).toUpperCase());
			
			if(!CommonService.IsNull(procCreatedonOnEnd))
				q.setParameter("procCreatedonOnEnd", CommonService.IsNullRString(procCreatedonOnEnd).toUpperCase());
			
			q.setParameter("p_orderby", "ACS");
			
			List<SyncQueue> lst = q.list();
			
			session.flush();
			
			if(CommonService.getSize(lst) > 0 )			
				return Collections.checkedList(lst, SyncQueue.class);
			else 
				return null;
			
		}catch(HibernateException ex) {
			logging.error(ex.getMessage());
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex1) {
			logging.error(ex1.getMessage());
			ex1.printStackTrace();
			throw ex1;
		}
		
	}
}
